const http = require("http");
const fs = require("fs");
const path = require("path");
const url = require("url");

//创建一个路径放html代码
var filePath = path.join(__dirname,"/index.html");
var writeable = fs.createWriteStream(filePath);

var reqUrl = 'http://nodejs.cn/';

// url.parse(url) 可以将url字符串转换为对象的结构
var obj = url.parse(reqUrl);

var options = {
    hostname: "nodejs.cn",
    port: 80,
    path: "/",
    method: "GET" //发数据：GET；提交数据：POST
}

var request = http.request(options, (res)=>{
    var str = "";
    res.on("data",(chunk)=>{
        str += chunk;
    })
    res.on("end",()=>{
        console.log(str);
        // 将HTML代码存到本地
        writeable.write(str);
        writeable.end();
        
    })
})


//http.request方法一定要调用end方法
request.end()

